local skill = fk.CreateSkill {
  name = "lvshi__dismantlement_skill",
}

local ls = require "packages/lvshi/utility"

skill:addEffect("cardskill", {
  prompt = "#lvshi__dismantlement_skill",
  target_num = 1,
  mod_target_filter = function(self, player, to_select, selected, card)
    return to_select ~= player and not to_select:isAllNude()
  end,
  target_filter = Util.CardTargetFilter,--(player, card, scheme, courage)
  on_use = function(self, room, use)
    local choice = ls.strategytrickuse(use.from, use.card, "dismantlement_scheme", "dismantlement_courage")
    use.extra_data = use.extra_data or {}
    use.extra_data.strategy = choice
  end,
  on_effect = function(self, room, effect)
    if effect.from.dead or effect.to.dead or effect.to:isAllNude() then return end
    local cid
    if effect.extra_data.strategy == "dismantlement_scheme" then
      local cards = {}
      if effect.to:getHandcardNum() > 0 then table.insertIfNeed(cards, {"$Hand", effect.to:getCardIds("h")}) end
      if #effect.to:getCardIds("e") > 0 then table.insertIfNeed(cards, {"$Equip", effect.to:getCardIds("e")}) end
      if #effect.to:getCardIds("j") > 0 then table.insertIfNeed(cards, {"$Judge", effect.to:getCardIds("j")}) end
      cid = room:askToChooseCard(effect.from, { target = effect.to, flag = { card_data = cards }, skill_name = "dismantlement" })
    else
      cid = room:askToChooseCard(effect.from, { target = effect.to, flag = "hej", skill_name = "dismantlement" })
    end
    room:throwCard({cid}, "dismantlement_skill", effect.to, effect.from)
    if effect.extra_data.strategy == "dismantlement_courage" then
      if effect.from:distanceTo(effect.to) == 1 and Fk:getCardById(cid).color == effect.card.color then
        room:delay(200)
        room:moveCardTo(cid, Card.PlayerHand, effect.from, fk.ReasonJustMove, "dismantlement_skill", nil, true, effect.from)
      end
    end
  end,
})

return skill
